﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace SettlersEngine
{
    /// <summary>
    /// Interaction logic for AnimationPlayer.xaml
    /// </summary>
    public partial class AnimatedElement : ContentControl
    {
        internal AnimationPlayer Player { get; private set; }
        internal AnimationClass Class { get; private set; }

        public static DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(String), typeof(AnimatedElement));
        internal static AnimationLibrary Library { get; private set; }

        public String Source
        {
            get { return (String)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public static void LoadLibrary(Uri inPath)
        {
            Library = AnimationLibrary.OpenFromDirectory(inPath.OriginalString);
        }
             
        public AnimatedElement()
        {
            InitializeComponent();

            DependencyPropertyDescriptor.FromProperty(SourceProperty, typeof(AnimatedElement)).AddValueChanged(this, OnSourceChanged);
        }

        protected void OnSourceChanged(object sender, EventArgs args)
        {
            Class = Library.FindClass(Source);

            if (Player != null)
                Player.Detach();

            Player = null;
            Player = new AnimationPlayer(Class);

            Player.Attach(this);
        }

        public void Play(String inAnimSet)
        {
            Player.Play(Class.FindSet(inAnimSet));
        }
    }
}
